package com.yzw.controller;

import com.alibaba.excel.EasyExcel;
import com.yzw.listeners.ExcelDataListener;
import com.yzw.service.OmsBcReceivableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;


/**
 * @author yw
 * @date 2023/12/19
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private OmsBcReceivableService omsBcReceivableService;
    /**
     * sn导入 大文件异步导入
     */
    @PostMapping(value = "/importExcel")
    public Object importInitCloudShare(@RequestParam("file") MultipartFile file) throws IOException {
        logger.info("云存数据初始化插入入库");
        if (file == null) {
            throw new IllegalArgumentException("导入的文件为空");
        }
        String filePath = file.getOriginalFilename();
        if (StringUtils.isEmpty(filePath)){
            throw new IllegalArgumentException("文件名不能为空");
        }
        if (!(filePath.endsWith(".xls") || filePath.endsWith(".xlsx"))) {
            throw new IllegalArgumentException("请导入Excel文件");
        }
        logger.info("importExcel开始时间：start：{}", System.currentTimeMillis());
        ExcelDataListener listener = new ExcelDataListener(omsBcReceivableService);
        EasyExcel.read(file.getInputStream(), OmsBcReceivableModel.class, listener).sheet().doRead();

        logger.info("上传成功, time:{}", System.currentTimeMillis());
        return "success";
    }
}
